package com.commerce.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.commerce.common.utils.PageResult;
import com.commerce.common.entity.Product;
import com.commerce.product.mapper.ProductMapper;
import com.commerce.product.service.ProductService;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {

    @Override
    public PageResult<Product> getPageList(int page, int size, Long categoryId, Long[] brandIds, 
                                         Integer minPrice, Integer maxPrice, String keyword, 
                                         Integer isHot, Integer isNew, String sort) {
        Page<Product> pageParam = new Page<>(page, size);
        Page<Product> pageResult = (Page<Product>) baseMapper.selectProductPage(pageParam, categoryId, brandIds,
                                                              minPrice, maxPrice, keyword, 
                                                              isHot, isNew, sort);
        return PageResult.of(pageResult.getRecords(), pageResult.getTotal(), page, size);
    }

    @Override
    public Product getProductById(Long id) {
        return getById(id);
    }

    @Override
    public Long addProduct(Product product) {
        product.setCreateTime(LocalDateTime.now());
        product.setUpdateTime(LocalDateTime.now());
        boolean success = save(product);
        return success ? product.getId() : null;
    }

    @Override
    public boolean updateProduct(Product product) {
        product.setUpdateTime(LocalDateTime.now());
        return updateById(product);
    }

    @Override
    public boolean deleteProduct(Long id) {
        // 逻辑删除，将状态设置为0（下架/删除状态）
        Product product = getById(id);
        if (product == null) {
            return false;
        }
        product.setStatus(0);  // 0表示下架/删除状态
        product.setUpdateTime(LocalDateTime.now());
        return updateById(product);
    }

    @Override
    public List<Product> getHotProducts(int limit) {
        LambdaQueryWrapper<Product> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Product::getStatus, 1)
               .eq(Product::getIsHot, 1)
               .orderByDesc(Product::getSales)
               .last("LIMIT " + limit);
        return list(wrapper);
    }

    @Override
    public List<Product> getNewProducts(int limit) {
        LambdaQueryWrapper<Product> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Product::getStatus, 1)
               .eq(Product::getIsNew, 1)
               .orderByDesc(Product::getCreateTime)
               .last("LIMIT " + limit);
        return list(wrapper);
    }

    @Override
    public List<Product> getRecommendProducts(int limit) {
        LambdaQueryWrapper<Product> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Product::getStatus, 1)
               .eq(Product::getIsRecommend, 1)
               .orderByDesc(Product::getSales)
               .last("LIMIT " + limit);
        return list(wrapper);
    }
} 